﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace HiUpdateTools.Core
{
    public abstract class XmlSerializationBase
    {
        // Methods
        protected XmlSerializationBase()
        {
        }

        public static XmlSerializationBase Load(Stream fs, Type objectType)
        {
            return Load(fs, objectType, true);
        }

        public static XmlSerializationBase Load(TextReader reader, Type objectType)
        {
            return Load(reader, objectType, true);
        }

        public static XmlSerializationBase Load(string sFilePath, Type objectType)
        {
            if (File.Exists(sFilePath))
            {
                using (StreamReader str = new StreamReader(sFilePath))
                {
                    return Load(str, objectType, true);
                }
            }
            return null;
        }

        public static XmlSerializationBase Load(XmlReader reader, Type objectType)
        {
            return Load(reader, objectType, true);
        }

        public static XmlSerializationBase Load(Stream fs, Type objectType, bool AutoClose)
        {
            XmlSerializer serializer = null;
            XmlSerializationBase base2;
            try
            {
                serializer = new XmlSerializer(objectType);
                base2 = (XmlSerializationBase)serializer.Deserialize(fs);
            }
            finally
            {
                if (AutoClose)
                {
                    fs.Close();
                }
            }
            return base2;
        }

        public static XmlSerializationBase Load(TextReader reader, Type objectType, bool AutoClose)
        {
            XmlSerializer serializer = null;
            XmlSerializationBase base2;
            try
            {
                serializer = new XmlSerializer(objectType);
                base2 = (XmlSerializationBase)serializer.Deserialize(reader);
            }
            finally
            {
                if (AutoClose)
                {
                    reader.Close();
                }
            }
            return base2;
        }

        public static XmlSerializationBase Load(XmlReader reader, Type objectType, bool AutoClose)
        {
            XmlSerializer serializer = null;
            XmlSerializationBase base2;
            try
            {
                serializer = new XmlSerializer(objectType);
                base2 = (XmlSerializationBase)serializer.Deserialize(reader);
            }
            finally
            {
                if (AutoClose)
                {
                    reader.Close();
                }
            }
            return base2;
        }

        public void Save(FileStream fs)
        {
            this.Save(fs, true);
        }

        public void Save(TextWriter writer)
        {
            this.Save(writer, true);
        }

        public void Save(string sFilePath)
        {
            using (StreamWriter str = new StreamWriter(sFilePath, false, Encoding.UTF8))
            {
                this.Save(str, true);
            }
        }

        public void Save(XmlWriter writer)
        {
            this.Save(writer, true);
        }

        public void Save(FileStream fs, bool AutoClose)
        {
            try
            {
                new XmlSerializer(base.GetType()).Serialize((Stream)fs, this);
            }
            finally
            {
                if (AutoClose)
                {
                    fs.Close();
                }
            }
        }

        public void Save(TextWriter writer, bool AutoClose)
        {
            try
            {
                new XmlSerializer(base.GetType()).Serialize(writer, this);
            }
            finally
            {
                if (AutoClose)
                {
                    writer.Close();
                }
            }
        }

        public void Save(XmlWriter writer, bool AutoClose)
        {
            try
            {
                new XmlSerializer(base.GetType()).Serialize(writer, this);
            }
            finally
            {
                if (AutoClose)
                {
                    writer.Close();
                }
            }
        }

        public string ToXml()
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(base.GetType());
                MemoryStream stream = new MemoryStream();
                serializer.Serialize((Stream)stream, this);
                stream.Position = 0L;
                return Encoding.UTF8.GetString(stream.GetBuffer(), 0, Convert.ToInt16(stream.Length));
            }
            catch
            {
                return "";
            }
        }

        public XmlDocument ToXmlDocument()
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml(this.ToXml());
            return document;
        }


    }

}
